const express = require('express');
const app = express();
const mongoose = require('mongoose');
app.use(require('cors')()); //解决跨域问题
app.use(express.json());
app.get('/',async(req,res)=>{
    res.send("a");
})

// 业务逻辑
// 显示学生列表信息
// app.get('/api/studentList',async(req,res)=>{
//     const students = await Student.find().sort({snumber:"asc"});
//     res.send(students);
// })
// 分页查询所有学生列表
app.all('/api/studentList',async(req,res,next)=>{
 // 返回结果
    const result = {
        data:{},
        total:'',
    };
    // 总记录数
    Student.count({},(err,counts)=>{
        if(err){
            res.json(err);
        }else{
            result.total = counts
            console.log('记录总数为'+counts);
        }   
    })
    // 第几页数据
    var PageSize = parseInt(req.query.pageSize);
    var CurPage  = parseInt(req.query.curPage);

    Student.find({},(err,data)=>{
        result.data = data;
        res.send(result);
    }).skip((CurPage-1)*PageSize).limit(PageSize).sort({snumber:"asc"});
})
//录入学生：
app.post('/api/students',async(req,res)=>{
    //console.log(req.body);  包含请求的表单内容 
    const student = await Student.create(req.body);
    res.send(Student);
})
// 删除学生：
app.delete('/api/student/:id',async(req,res)=>{
    console.log(req.params.id);
    await Student.findByIdAndDelete(req.params.id)
    //返回
    res.send({
        status:true
    })

})
//通过学号精确查询
app.get('/api/findByNumber/:xuehao',async(req,res)=>{
    console.log(req.params.xuehao);
    const student = await Student.find({snumber:req.params.xuehao});
    res.send(student);
})
//通过名字模糊查询
// app.get('/api/findByName/:name',async(req,res)=>{
//     console.log(req.params);
//     const query = new RegExp(req.params.name);
//     const students = await Student.find({name:query}).sort({snumber:"asc"})
//    res.send(students);
// })
// 更新学生信息
app.put('/api/update/:id',async(req,res)=>{
    const student = await Student.findByIdAndUpdate(req.params.id,req.body);
    res.send(student);
})

// 模糊分页查询通过学生姓名
app.get('/api/findByName',async (req,res)=>{
    // 返回结果
    const result = {
        data:{},
        total:'',
    }
    console.log(req.query)
    // 查询条件
    const condition = new RegExp(req.query.name);
    // 总记录数
    Student.count({name:condition},(err,count)=>{
        if(err){
            res.json(err);
        }else{
            result.total = count
            console.log('记录总数为'+count);
        }
    })
    // 第几页数据
    var PageSize = parseInt(req.query.pageSize);
    var CurPage  = parseInt(req.query.curPage);

    Student.find({},(err,data)=>{
        result.data = data;
        res.send(result);
    }).where({name:condition}).skip((CurPage-1)*PageSize).limit(PageSize).sort({snumber:"asc"});
})
// 连接数据库
mongoose.connect("mongodb://localhost:27017/studentM",{
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true
})
const Student = mongoose.model('Student',new mongoose.Schema({
    snumber:{type:String},
    name:{type:String},
    class:{type:String},
    sex:{type:String},
    clan:{type:String},
    javalan:{type:String},
    dblan:{type:String},

}))

app.listen(3001,function(){
    console.log("success");
})